package com.zhss.massivedata.order.domain;

import com.zhss.massivedata.order.constant.PayType;

import java.util.*;

/**
 * 订单构建工厂
 */
public class OrderFactory {

    public static OrderInfo build() {
        // 构建订单id
        long orderInfoId = randomId();

        // 构建随机个数的订单条目
        List<OrderItem> orderItems = new ArrayList<OrderItem>();
        for(int i = 0; i < randomCount(); i++) {
            orderItems.add(buildOrderItem(orderInfoId));
        }

        // 构建订单
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderInfoId(orderInfoId);
        orderInfo.setOrderNo("订单编号" + randomString());
        orderInfo.setUserAccountId(randomId());
        orderInfo.setUsername("用户名称" + randomString());
        orderInfo.setConsignee("收货人" + randomString());
        orderInfo.setDeliveryAddress("收货地址" + randomString());
        orderInfo.setConsigneeCellPhoneNumber("收货人手机号码" + randomString());
        orderInfo.setFreight(randomDouble());
        orderInfo.setPayType(PayType.WEICHAT_PAY);
        orderInfo.setTotalAmount(randomDouble());
        orderInfo.setDiscountAmount(randomDouble());
        orderInfo.setCouponAmount(randomDouble());
        orderInfo.setPayableAmount(randomDouble());
        orderInfo.setInvoiceTitle("发票抬头" + randomString());
        orderInfo.setTaxpayerId("纳税人识别号" + randomString());
        orderInfo.setOrderComment("订单备注" + randomString());
        orderInfo.setIsPublishedComment(1);
        orderInfo.setConfirmReceiptTime(null);
        orderInfo.setCouponId(randomId());
        orderInfo.setGmtCreate(now());
        orderInfo.setGmtModified(now());
        orderInfo.setOrderItems(orderItems);

        return orderInfo;
    }

    private static OrderItem buildOrderItem(long orderInfoId) {
        OrderItem orderItem = new OrderItem();
        orderItem.setId(randomId());
        orderItem.setOrderInfoId(orderInfoId);
        orderItem.setGoodsId(randomId());
        orderItem.setGoodsSkuId(randomId());
        orderItem.setGoodsSkuCode("商品sku编号" + randomString());
        orderItem.setGoodsName("商品名称" + randomString());
        orderItem.setSaleProperties("销售属性" + randomString());
        orderItem.setGoodsGrossWeight(randomDouble());
        orderItem.setPurchaseQuantity(randomCount());
        orderItem.setPurchasePrice(randomDouble());
        orderItem.setPromotionActivityId(randomId());
        orderItem.setGoodsLength(randomDouble());
        orderItem.setGoodsWidth(randomDouble());
        orderItem.setGoodsHeight(randomDouble());
        orderItem.setGmtCreate(now());
        orderItem.setGmtModified(now());

        return orderItem;
    }

    private static long randomId() {
        Random random = new Random();
        return random.nextInt(1000000) + 1;
    }

    private static long randomCount() {
        Random random = new Random();
        return random.nextInt(10) + 1;
    }

    private static double randomDouble() {
        Random random = new Random();
        return random.nextInt(1000) + 1;
    }

    private static String randomString() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    private static Date now() {
        return new Date();
    }

}
